package com.diandiansong.manager.ui.user;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.diandiansong.AppUpdateManager;
import com.diandiansong.app.App;
import com.diandiansong.app.R;
import com.diandiansong.app.utils.Api;
import com.diandiansong.manager.entity.SalesLoginInfo;
import com.diandiansong.manager.ui.Main;
import com.diandiansong.manager.utils.M;

import butterknife.BindView;
import butterknife.OnClick;
import diandiansong.library.ui.base.BaseAct;
import diandiansong.library.utils.CCallBack;

/**
 * 登录
 */
public class Login extends BaseAct {

    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;

    public static void start(Context context) {
        Intent starter = new Intent(context, Login.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addContentView(R.layout.manager_login);
        initTitle("登录");
        new AppUpdateManager(this, Api.APP_UPDATE_URL_MERCHANTS, false,mEtPhone).checkUpdate();
        if (App.DEBUG) {
            //业务员：  账号：13912312312    密码：123123
            //主管   账号：13912312313   密码：123123
            //mEtPhone.setText("13912312313");
            //mEtPwd.setText("123123");

            //13912312313   123123 主管
            //13735567661   123456 业务
            mEtPhone.setText("13735567661");
            mEtPwd.setText("123456");
        }


        if (SalesLoginInfo.isLogined(getActivity())) {
            Api.TOKEN = SalesLoginInfo.getToken(getActivity());
            logger.d(Api.TOKEN);
            Main.start(getActivity());
            finish();
        }
    }

    @OnClick({R.id.do_login, R.id.go_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.do_login:
                String phone = getText(mEtPhone);
                String pwd = getText(mEtPwd);
                M.salesLogin(phone, pwd, this, new CCallBack<SalesLoginInfo>() {
                    @Override
                    public void onSuccess(SalesLoginInfo salesLoginInfo) throws Exception {
                        SalesLoginInfo.saveSalesLoginInfo(getActivity(), salesLoginInfo);
                        Main.start(getActivity());
                        Api.TOKEN = SalesLoginInfo.getToken(getActivity());
                        finish();
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        toast(msg);
                    }
                });
                break;

            case R.id.go_pwd:
                ForgetPwd.start(getActivity());
                break;
        }
    }
}
